package com.gitee.wsl.compose.ui.base

import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.PathEffect
import androidx.compose.ui.unit.dp
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Immutable
import androidx.compose.ui.unit.Dp



object Line

@Composable
fun Line.VerticalDottedLine() {
    val pathEffect = PathEffect.dashPathEffect(floatArrayOf(10f, 10f), 0f)
    Canvas(
        Modifier
            .fillMaxHeight()
            .width(1.dp)) {
        drawLine(
            color = Color.Red,
            start = Offset(0f, 0f),
            end = Offset(0f, size.height),
            pathEffect = pathEffect
        )
    }
}

@Composable
fun Line.HorizontalDottedLine() {
    val pathEffect = PathEffect.dashPathEffect(floatArrayOf(10f, 10f), 0f)
    Canvas(
        Modifier
            .fillMaxWidth()
            .height(1.dp)) {
        drawLine(
            color = Color.Red,
            start = Offset(0f, 0f),
            end = Offset(size.width, 0f),
            pathEffect = pathEffect
        )
    }
}


@Composable
fun Line.HorizontalDashedLine(
    modifier: Modifier = Modifier,
    colors: LineColors = LineDefaults.lineColors(),
    sizes: LineSizes = LineDefaults.lineSizes(),
) {
    Canvas(modifier = modifier.fillMaxWidth().height(sizes.thickness())) {
        drawLine(
            color = colors.strokeColor(),
            strokeWidth = sizes.thickness().toPx(),
            start = Offset(0f, sizes.thickness().toPx() / 2),
            end = Offset(size.width, sizes.thickness().toPx() / 2),
            pathEffect = PathEffect.dashPathEffect(
                intervals = floatArrayOf(sizes.dashOn().toPx(), sizes.dashOff().toPx()),
                phase = 0f,
            ),
        )
    }
}

@Composable
fun Line.VerticalDashedLine(
    modifier: Modifier = Modifier,
    colors: LineColors = LineDefaults.lineColors(),
    sizes: LineSizes = LineDefaults.lineSizes(),
) {
    Canvas(modifier = modifier.fillMaxHeight().width(sizes.thickness())) {
        drawLine(
            color = colors.strokeColor(),
            strokeWidth = sizes.thickness().toPx(),
            start = Offset(sizes.thickness().toPx() / 2, 0f),
            end = Offset(sizes.thickness().toPx() / 2, size.height),
            pathEffect = PathEffect.dashPathEffect(
                intervals = floatArrayOf(sizes.dashOn().toPx(), sizes.dashOff().toPx()),
                phase = 0f,
            ),
        )
    }
}

object LineDefaults {

    @Composable
    fun lineColors(
        strokeColor: Color = MaterialTheme.colorScheme.primary,
    ): LineColors = LineColors(
        strokeColor = strokeColor,
    )

    @Composable
    fun lineSizes(
        dashOn: Dp = 4.dp,
        dashOff: Dp = 4.dp,
        thickness: Dp = 1.dp,
    ): LineSizes = LineSizes(
        dashOn = dashOn,
        dashOff = dashOff,
        thickness = thickness,
    )
}

@Immutable
class LineColors internal constructor(
    private val strokeColor: Color,
) {
    internal fun strokeColor(): Color = strokeColor
}

@Immutable
class LineSizes internal constructor(
    private val dashOn: Dp,
    private val dashOff: Dp,
    private val thickness: Dp,
) {
    internal fun dashOn(): Dp = dashOn

    internal fun dashOff(): Dp = dashOff

    internal fun thickness(): Dp = thickness
}
